(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.feeCode.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'feeCodeService', 'commonService', 'ajaxServiceFactory' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, feeCodeService, commonService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("feeCodeList"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getFeeTypeOptions: {
				url : 'do/cda/dataList/getFeeTypeOptions',
				method : 'get'
			},
		});
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.setupDataStatusOptions = commonService.setupDataStatusOptions();
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			
			ajaxService.getFeeTypeOptions().then(function(response) {
				return $q(function(resolved, rejected) {
					ctrl.feeTypeOptions = response.data;
					resolved();
				});
			}, angular.noop).then(function() {
				feeCodeService.initListFilterData().then(function(response) {
					ctrl.feeCalcTypeOptions = response || [];
					ctrl.searchFeeCode();
				});
			});
		};

		ctrl.viewFeeCode = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.feeCode.view', ctrl.selectedRows[0]);
		};

		ctrl.createFeeCode = function() {
			$scope.go('cda.feeCode.create');
		};

		ctrl.updateFeeCode = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.feeCode.update', ctrl.selectedRows[0]);
		};

		ctrl.deleteFeeCode = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.feeCode.delete', ctrl.selectedRows[0]);
		};

		ctrl.searchFeeCode = function() {
			ctrl.searchCriteria.list = [];
			authService.getList(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.searchFeeCode();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.searchFeeCode();
		}
	}
})(angular);